﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QHBoxLayout"
#include "QVBoxLayout"
#include "QLabel"
#include <QDebug>
#include <QPropertyAnimation>
#include "loadqss.h"
#include <QString>
#include <QTextCodec>
#include <QTreeWidget>
#define X_OFF 200
#define Y_OFF 65
#define STR2HEX "STR-HEX互转"
#define STR2UTF16_8 "STR-UTF16_8"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->resize(820,600);
    this->setMinimumSize(820,600);
    lable1 = new QLabel("CQTools", this);
    lable1->setAlignment(Qt::AlignCenter);
    lable1->resize(X_OFF,Y_OFF);

    lable2 = new QLabel("首页", this);
    lable2->setAlignment(Qt::AlignLeft|Qt::AlignVCenter);
    lable2->resize(this->width()-X_OFF,Y_OFF);
    lable2->move(X_OFF,0);
    lable2->setAttribute(Qt::WA_Hover, true); //开启悬停事件
    lable2->installEventFilter(this); //安装事件过滤器

    lable3 = new QLabel("版本号 v1.0.2", lable2);
    lable3->move(lable2->width()-lable3->width(), lable2->height()-lable3->height());
    pAnimation = new QPropertyAnimation(lable3, "geometry");
    pAnimation->setDuration(2000);
    pAnimation->setEasingCurve(QEasingCurve::OutBounce);  // 缓和曲线风格
    lable1->setObjectName("lable1");
    lable2->setObjectName("lable2");
    lable3->setObjectName("lable3");
    LoadQss::loadQss(QString(":/qss/default.qss"));
    lable3->show();
    lable3->hide();

    //create sub page
    m_homePage = new homeWidget();
    m_str2hex = new StrConvertHex();
    m_str2utf = new Str2UnicodeUtf8();

    m_treenavigation = new TreeNavigation(this);
    m_treenavigation->move(0, Y_OFF);
    m_treenavigation->resize(X_OFF,this->height());

    QStringList namelist;
    //添加首页功能
    QString home = QString("首页");
    m_treenavigation->addNode(home, namelist);
    pageMap.insert(home, m_homePage);
    namelist.clear();

    //添加文本类功能
    namelist<<STR2HEX<<STR2UTF16_8;
    m_treenavigation->addNode(QString("文本类"), namelist);
    pageMap.insert(STR2HEX, m_str2hex);
    pageMap.insert(STR2UTF16_8, m_str2utf);
    namelist.clear();

//    m_stackwidget = new QStackedWidget(this);
//    m_stackwidget->move(X_OFF, Y_OFF);
//    m_stackwidget->resize(this->width()-X_OFF, this->height()-Y_OFF);
//    m_stackwidget->addWidget(m_homePage);
//    m_stackwidget->addWidget(m_str2hex);
//    m_stackwidget->addWidget(m_str2utf);
//    m_stackwidget->setCurrentWidget(m_str2utf);

    m_slidingstackwidget = new SlidingStackedWidget(this);
    m_slidingstackwidget->move(X_OFF, Y_OFF);
    m_slidingstackwidget->resize(this->width()-X_OFF, this->height()-Y_OFF);
    m_slidingstackwidget->addWidget(m_homePage);
    m_slidingstackwidget->addWidget(m_str2hex);
    m_slidingstackwidget->addWidget(m_str2utf);
    m_slidingstackwidget->setCurrentWidget(m_str2utf);
    m_slidingstackwidget->setSpeed(350);
    m_slidingstackwidget->setVerticalMode(true);
    m_slidingstackwidget->setAnimation(QEasingCurve::OutBack);

    connect(m_treenavigation->treeWidget, &QTreeWidget::itemClicked, this, [this](QTreeWidgetItem *item, int column){
        qDebug()<<item->text(column);
        QTreeWidgetItem *parent = item->parent();
        if(NULL==parent) //注意：最顶端项是没有父节点的，双击这些项时注意(陷阱)
        {

            if(item->text(column) == "首页"){
                lable2->setText(item->text(column));
//                m_slidingstackwidget->setCurrentWidget(pageMap.value(item->text(column)));
                m_slidingstackwidget->slideInIdx(m_slidingstackwidget->indexOf(pageMap.value(item->text(column))));
            }
            qDebug()<<item->text(column)<<"is root node"<<endl;;
            return;
        }
        lable2->setText(item->text(column));
//        m_slidingstackwidget->setCurrentWidget(pageMap.value(item->text(column)));
        m_slidingstackwidget->slideInIdx(m_slidingstackwidget->indexOf(pageMap.value(item->text(column))));
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    lable2->resize(this->width()-X_OFF,Y_OFF);
    lable3->move(lable2->width()-lable3->width(), lable2->height()-lable3->height());
    m_treenavigation->resize(X_OFF,this->height());
    m_slidingstackwidget->resize(this->width()-X_OFF, this->height()-Y_OFF);
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == lable2){
        if (event->type()== QEvent::HoverEnter){
            lable3->show();
            old_x = lable3->x();
            old_y = lable3->y();
            pAnimation->setStartValue(QRect(lable3->x()-10, lable3->y(), lable3->width(), lable3->height()));
            pAnimation->setEndValue(QRect(lable3->x(), lable3->y(), lable3->width(), lable3->height()));
            pAnimation->start();
            return  true;
        }
        if (event->type()== QEvent::HoverLeave){
            pAnimation->stop();
            lable3->move(old_x, old_y);
            lable3->hide();
            return  true;
        }
    }
    return QMainWindow::eventFilter(obj, event);
}

